﻿<html>

	<head>
		<title>新增商品</title>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
		<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">

		<link href="../../css/skin/default/style.css" rel="stylesheet" type="text/css" />
		<link href="../../css/dialog.css" rel="stylesheet">
		<link href="../../js/lib/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />

		<script src="../../js/lib/jquery/jquery-1.10.2.min.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/ligerui.all.js" type="text/javascript"></script>

		<script src="../../js/lib/ligerUI/js/plugins/ligerPopupEdit.js"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerLayout.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerGrid.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerForm.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerCheckBox.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerComboBox.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerDateEditor.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerRadio.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerTextBox.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerSpinner.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerTree.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerDialog.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerDrag.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerResizable.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerTip.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerMenu.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerToolBar.js" type="text/javascript"></script>
		<script type="text/javascript" src="../../js/lhgdialog/lhgdialog.js?skin=idialog"></script>

		<script src="../../js/lib/jquery.form.js" type="text/javascript"></script>
		<script src="../../js/lib/json2.js" type="text/javascript"></script>

		<script type="text/javascript" src="../../js/JScripts/kindeditor/kindeditor-all.js"></script>
		<!--<script type="text/javascript" src="../../js/JScripts/kindeditor/lang/zh_CN.js"></script>-->

		<script src="../../js/dialog.js"></script>
		<script src="../../js/JS/layout.js"></script>
		<script src="../../js/constants.js" type="text/javascript"></script>
		<script src="../../js/api.js" type="text/javascript"></script>
		<style>
			a:hover {
				cursor: pointer;
			}
			
			.style1 {
				height: 21px;
			}
			
			body {
				height: 100%;
			}
		</style>
		<script type="text/javascript">
			var productUrl = BaseUrl + "Product?token=" + $api.getStorage("token");
			var Fid = $api.getparastr("id");
			$(function() {
				$("form").ligerForm();
				initContorl();
				if(Fid) {
					initView(Fid);
				} else
					showGird();

			});

			var popPtype;
			//初始化控件
			function initContorl() {
				popPtype = $("#PType").ligerPopupEdit({
					valueField: 'PID',
					textField: 'Name',
					width: 160,
					onEnterDown: function(e) {
						parent.showPTypeDialog(function(row) {
							popPtype.setValue(row.Fid);
							popPtype.setText(row.Fname);

						});
					},
					onButtonClick: function() {
						parent.showPTypeDialog(function(row) {
							popPtype.setValue(row.Fid);
							popPtype.setText(row.Fname);

						});
					}

				});

			}
			var brandid;
			//初始化界面
			function initView(Fid) {
				var url = productUrl + "&Fpid=" + Fid;
				$api.ajaxget(url, {}, function(data) {
					$api.loadJsonToForm(data.Product);
					if(data.Fstatus == -11) {
						$("#div-alarm").text(data.Freason);
					} else {
						$("#div-alarm").css("display", "none");
					}
					popPtype.setValue(data.Product.Ftypeid);

					if(data.Product.Fvideo) {
						$("#video").attr("src", data.Product.Fvideo);
						$("#video").css("display", "block");

						$("#ImgFvDel").css("display", "block");
					}
					initAttach(data.Attrlist);

					showGird();
				});
			}
			///////////初始化附件//////////////////////////////////////////////
			function initAttach(rows) {
				for(var i = 0; i < rows.length; i++) {
					var path = rows[i];
					var k = i + 1;
					$("#Img" + k).attr("src", path);
					$("#Img" + k).css('display', 'block');
					$("#imgdel" + k).css('display', 'block');
				}

			}

			function showimg(obj) {
				var src = $(obj).attr("src");
				if(djpg != src) {
					src = src.replace(small, big);
					$.ndialog({
						lock: true,
						title: "图片浏览",
						content: "<img src=\"" + src + "\" />",
						padding: 0,
						min: false,
						max: false
					});
					return;
				}
				parent.showPhotoDialog();

			}

			function showVideoDialog() {
				parent.openCommWindow('vcommon/common/VideoList.html', "添加视频", 880, 580, 'video');
			}

			function delImg(i) {
				if(i == 0) {
					$("#video").attr("src", "");
					$("#video").css("display", "none");
					$("#ImgFvDel").css("display", "none");
					return;
				}
				$("#Img" + i).attr("src", djpg);
				$("#imgdel" + i).css('display', 'none');
			}

			function putPhotos(photos, cmd) {
				if(photos.length == 0) {
					return;
				}

				for(var i = 1; i < 6; i++) {
					var src = $("#Img" + i).attr("src");
					if(src == djpg) {
						if(photos.length == 0) {
							break;
						}
						var selectsrc = photos.pop();

						$("#Img" + i).attr("src", selectsrc);
						$("#Img" + i).css('display', 'block');
						$("#imgdel" + i).css('display', 'block');
					}
				}
			}
			////////////////////SKU设置//////////////////////////////////////////			
			var manager;

			function showGird(data) {
				var url = BaseUrl + "/productprice/sql?token=" + $api.getStorage("token") + "&tid=" + tid;
				manager = $("#maingrid4").ligerGrid({
					columns: [{
							display: '国别',
							name: 'Fname',
							minWidth: 120
						},
						{
							display: '单价',
							name: 'Fprice',
							width: 100
						},
						{
							display: '结算币种',
							name: 'Fcurrency',
							width: 120
						}
					],
					dataAction: 'server',
					url: url,
					width: '400',
					rowHeight: 100,
					usePager: false,
					onDblClickRow: function(selectRow, rowindex, rowobj) {

						parent.openCommWindow2('vstore/tb_product/AddPrice.html?data=' +
							escape(JSON.stringify(selectRow)), "设置多国价格", 480,
							320, callDo);
					}
				});
			}

			function callDo(res) {
				if(Fid) {
					let row = res.row;
					if(row.Fid != 0) {
						row.Fpid = Fid;
						var url = BaseUrl + "productprice/set?token=" + $api.getStorage("token") + "&action=edit";
						$api.ajaxapp2(url, row, function(data) {
							if(data.Message) $api.showtoast(data.Message);
							manager.updateRow(selectRow, row);
							if($api.isEmpty(Fid))
								doSaveInfo();
						});
					} else {
						var url = BaseUrl + "productprice/set?token=" + $api.getStorage("token") + "&action=add";
						row.Fpid = Fid;
						$api.ajaxapp2(url, row, function(data) {
							row.Fid = Number(data.Message);
							manager.addRow(
								row
							);
							if($api.isEmpty(Fid))
								doSaveInfo();
						});
					}
				} else {
					if(row.Fid != 0)
						manager.updateRow(selectRow, row);
					else
						manager.addRow(
							row
						);
				}
			}

			function addprice() {
				parent.openCommWindow('vstore/tb_product/AddPrice.html', "设置多国价格", 480,
					320, callDo);
			}

			var selectRow;
			function editRow(i) {
				parent.openCommWindow2('vstore/tb_product/AddPrice.html?data=' +
					escape(JSON.stringify(selectRow)), "设置多国价格", 480,
					320, callDo);
			}

			function deleteRow(i) {
				$.ligerDialog.confirm("价格删除后不能恢复，\n您确定要移除？", function(yes) {
					if(yes) {
						if(Fid) {
							var row = manager.getRow(i);
							var url = BaseUrl + "productprice/set?token=" + $api.getStorage("token") + "&action=del";
							$api.ajaxapp2(url, row, function(data) {
								manager.deleteRow(i);
							});
						} else {
							manager.deleteRow(i);
						}
					}
				});

			}

			function saveInfo(row, cmd) {
				if(cmd == 'video') {
					$("#video").attr("src", row.Fpath);
					$("#video").css("display", "block");

					$("#ImgFvDel").css("display", "block");
				}
			}

			//////////////////////////////////////////////////////////////////////////////
			function doSaveInfo() {

				var ret = $('form').serializeObject();

				var a1 = $("#Img1").attr("src");
				var a2 = $("#Img2").attr("src");
				var a3 = $("#Img3").attr("src");
				var a4 = $("#Img4").attr("src");
				var a5 = $("#Img5").attr("src");
				if(!$("#Fsummary").val()) {
					$.ligerDialog.warn('商品摘要不能为空');
					return;
				}

				if(!popPtype.getValue()) {
					$.ligerDialog.warn('请选择商品分类');
					return;
				}

				if(!$("#Fpcode").val()) {
					$.ligerDialog.warn('商品编码不能为空');
					return;
				}

				if(!$("#Fbarcode").val()) {
					$.ligerDialog.warn('商品条码不能为空');
					return;
				}
				var attachList = []
				for(var i = 1; i < 6; i++) {
					var path = $("#Img" + i).attr("src");
					if(path != djpg) {
						attachList.push(path);
					}
				}
				if(null == attachList || attachList.length == 0) {
					$.ligerDialog.warn('至少选取一张图片');
					return;
				} 
				ret.Fvideo = $("#video").attr("src");
				ret.Fhimg = attachList[0];
				ret.Ftypeid = ret.Ftypeid; 
				ret.Fstatus = 1;
				var arr = manager.getData();
				var param = {
					"product": ret,
					"attach": attachList,
					"ppList": arr
				}
				var url;
				if($api.isEmpty(Fid)) {
					url = productUrl + "&action=add";
				} else {
					ret.Fid = Fid;
					url = productUrl + "&action=edit";
				}

				console.log(param);
				console.log(JSON.stringify(param));
				$api.ajaxapp2(url, param, function(data) {
					$api.showtoast("提交成功");
					clear();
				});
			}

			function clear() {
				$("#Fsummary").val("");
				$("#Fpcode").val("");
				$("#Fbarcode").val("");
				$("#video").attr("src", "");
				$("#video").css("display", "none");
				$("#ImgFvDel").css("display", "none");
				for(var i = 1; i < 6; i++) {
					$("#Img" + i).attr("src", djpg);
					$("#imgdel" + i).css('display', 'none');
				} 
				for(var i in manager.getData()) {
					manager.deleteRow(i);
				} 
				Fid = null; 
			}

			function lastword() {
				var len = $("#Fsummary").val().length;

				if(len >= 30)
					$("#lnum").text("还可以输入0个字符");
				else if(len == 0)
					$("#lnum").text("标题最多30个字符");
				else
					$("#lnum").text("还可以输入" + (30 - len) + "个字符");
			}
		</script>
	</head>

	<body class="mainbody">
		<form>
			<!--导航栏-->
			<div class="location" width="300px">
				<table>
					<tr>
						<td class="style1">
							<span style="margin-left:10px">商品管理</span>
							<i class="arrow"></i>
							<span class="nows">发布商品</span> </td>
					</tr>
				</table>
			</div>

			<div class="line10"></div>
			<div class="div-alarm"></div>

			<!--/导航栏-->
			<div class="div-content">
				<div class="divhead">填写基本信息</div>
				<hr class="divline" />
				<dl>
					<dt>商品摘要</dt>
					<dd>
						<div style="float:left">
							<input type="text" onkeyup="lastword();" id="Fsummary" maxlength="30" name="Fsummary" ltype="text" ligerui="{width:380}" validate="{required:true,minlength:4,maxlength:30}" />
						</div>
						<div style="float:left"> <span class="Validform_checktip" id="lnum" style="float:left">*标题最多30个字符</span></div>
					</dd>
				</dl> 
				<dl>
					<dt>所属分类</dt>
					<dd>
						<input type="text" id="Ftype" name="Ftype" placeholder='所属分类' ligerui="{width:380}" />

					</dd>
				</dl>
				<dl ID="div_goods_no">
					<dt>商品货号</dt>
					<dd>
						<input type="text" id="Fpcode" name="Fpcode" ltype="text" ligerui="{width:380,initSelect:true}" validate="{required:true }" />

					</dd>
				</dl>
				<dl ID="div_Fbarcode">
					<dt>商品条码</dt>
					<dd>
						<div style="float:left">
							<input type="text" id="Fbarcode" name="Fbarcode" ltype="text" ligerui="{width:380,initSelect:true}" validate="{required:true }" />
						</div>
						<div style="float:left"> <span class="Validform_checktip">*商品条码必须是唯一值</span> </div>
					</dd>
				</dl>
			</div>
			<div class="div-content">
				<div class="divhead">产品销售属性</div>
				<hr class="divline" />
				<dl>
					<dt>原价:</dt>
					<dd>
						<div style="float:left">
							<input type="text" id="Foprice" name="Foprice" ltype="text" ligerui="{width:120,initSelect:true}" validate="{required:true}" />
						</div>

					</dd>
				</dl>
				<dl>
					<dt>售价:</dt>
					<dd>
						<div style="float:left">
							<input type="text" id="Fsaleprice" name="Fsaleprice" ltype="text" ligerui="{width:120,initSelect:true}" validate="{required:true}" />
						</div>

					</dd>
				</dl>
				<dl><dt> </dt>
					<dd>
						<div class="toolbar">
							<div class="l-list">
								<ul id="icon-list" class="icon-list">
									<li>
										<a class='add' onclick='addprice();'><i></i><span>添加多国价格</span></a>
									</li>
								</ul>
							</div>
						</div>
						<div id="maingrid4" style="margin: 0px;background:#fff;"></div>
					</dd>
				</dl>
				<dl>
					<dt>箱装数:</dt>
					<dd>
						<div style="float:left"> <input type="text" id="Ft1" name="Ft1" ltype="text" ligerui="{width:120,initSelect:true,number:true}" validate="{required:true}" />
						</div>
					</dd>
				</dl>
				<dl>
					<dt>大包装数:</dt>
					<dd>
						<div style="float:left"> <input type="text" id="Ft2" name="Ft2" ltype="text" ligerui="{width:120,initSelect:true,number:true}" validate="{required:true}" />
						</div>
					</dd>
				</dl>
				<dl>
					<dt>重量:</dt>
					<dd>
						<div style="float: left">
							毛重(克)
						</div>
						<div style="float:left;margin-left:5px;"> <input type="text" id="Fc2" name="Fc2" ltype="text" ligerui="{width:60,initSelect:true,number:true}" validate="{required:true}" />
						</div>
						<div style="float: left;margin-left:5px;">
							净重(克)
						</div>
						<div style="float:left;margin-left:5px;"> <input type="text" id="Fc7" name="Fc7" ltype="text" ligerui="{width:60,initSelect:true,number:true}" validate="{required:true}" />
						</div>
					</dd>
				</dl>
				<dl>
					<dt>体积:</dt>
					<dd>
						<div style="float: left">
							长(毫米)
						</div>
						<div style="float:left;margin-left:5px;">

							<input type="text" id="Ft2" name="Ft2" ltype="text" ligerui="{width:60,initSelect:true,number:true}" validate="{required:true}" />
						</div>
						<div style="float: left;margin-left:5px;">
							宽(毫米)
						</div>
						<div style="float:left;margin-left:5px;">
							<input type="text" id="Ft2" name="Ft2" ltype="text" ligerui="{width:60,initSelect:true,number:true}" validate="{required:true}" />
						</div>
						<div style="float: left;margin-left:5px;">
							高(毫米)
						</div>
						<div style="float:left;margin-left:5px;">
							<input type="text" id="Ft2" name="Ft2" ltype="text" ligerui="{width:60,initSelect:true,number:true}" validate="{required:true}" />
						</div>
					</dd>
				</dl>
			</div>
			<div class="div-content">

				<div class="divhead">图片和详细说明</div>
				<hr class="divline" />
				<dl>
					<dt>商品视频</dt>
					<dd>
						<div style="float:left;" class="videoContainer">

							<video id="video" src="" controls="controls">
								您的浏览器不支持 video 标签。
							</video>

							<img id="ImgFvDel" src="../../img/common_delete.png" class="closeimg" onclick="delImg(0);" />

						</div>

						<div style="float:left;margin-left:5px;">
							用于详情页视频展示非必填项<br/>
							<div onclick="showVideoDialog();" class="btn green">添加视频</div>
						</div>
					</dd>
				</dl>

				<dl>

					<dt>图片相册(最多上传5张)</dt>
					<dd>
						<table>
							<tr>
								<td>
									<div class="picContainer">
										<img id="Img1" src="../../css/skin/default/add_pic.png" onclick="showimg(this);" class="imgs" />
										<img id="imgdel1" src="../../img/common_delete.png" class="closeimg" onclick="delImg(1);" />
									</div>
								</td>
								<td>
									<div class="picContainer">
										<img id="Img2" src="../../css/skin/default/add_pic.png" onclick="showimg(this);" class="imgs" />
										<img id="imgdel2" src="../../img/common_delete.png" class="closeimg" onclick="delImg(2);" />
									</div>
								</td>
								<td>
									<div class="picContainer">
										<img id="Img3" src="../../css/skin/default/add_pic.png" onclick="showimg(this);" class="imgs" />
										<img id="imgdel3" src="../../img/common_delete.png" class="closeimg" onclick="delImg(3);" />
									</div>
								</td>
								<td>
									<div class="picContainer">
										<img id="Img4" src="../../css/skin/default/add_pic.png" onclick="showimg(this);" class="imgs" />
										<img id="imgdel4" src="../../img/common_delete.png" class="closeimg" onclick="delImg(4);" />
									</div>
								</td>
								<td>
									<div class="picContainer">
										<img id="Img5" src="../../css/skin/default/add_pic.png" class="imgs" />
										<img id="imgdel5" src="../../img/common_delete.png" class="closeimg" onclick="delImg(5);" />
									</div>
								</td>
							</tr>
						</table>
					</dd>
				</dl>

			</div>

			<div align="center" style="margin:20px;">
				<div style="margin-top:20px">
					<input style="width:150px;display:inline" type="button" id="pass" value="保存草稿" class="btn" onclick="doSaveInfo(0);" />
					<input style="width:150px;display:inline" type="button" id="unpass" value="提交审核" class="btn yellow" onclick="doSaveInfo(2);" />

				</div>
			</div>

		</form>
	</body>

</html>